// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.query;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.IAction;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.ui.forms.IFormPart;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2007-12-8 qiang.zhang $
 * 
 */
public abstract class AbstractFormPage extends FormPage {

    private final static String ID = "org.dengues.designer.ui.query.";

    public static final String ID_COLUMNS_ALIAS = ID + "columns";

    public static final String ID_FILTERS = ID + "filters";

    public static final String ID_GROUP_SOERT = ID + "groups";

    public static final String ID_HAVING = ID + "having";

    public static final String ID_UNION = ID + "union";

    // FORM BODY
    public static final int FORM_BODY_MARGIN_TOP = 12;

    public static final int FORM_BODY_MARGIN_BOTTOM = 12;

    public static final int FORM_BODY_MARGIN_LEFT = 6;

    public static final int FORM_BODY_MARGIN_RIGHT = 6;

    public static final int FORM_BODY_HORIZONTAL_SPACING = 20;

    // Should be 20; but, we minus 3 because the section automatically pads the
    // bottom margin by that amount
    public static final int FORM_BODY_VERTICAL_SPACING = 17;

    public static final int FORM_BODY_MARGIN_HEIGHT = 0;

    public static final int FORM_BODY_MARGIN_WIDTH = 0;

    // FORM PANE
    public static final int FORM_PANE_MARGIN_TOP = 0;

    public static final int FORM_PANE_MARGIN_BOTTOM = 0;

    public static final int FORM_PANE_MARGIN_LEFT = 0;

    public static final int FORM_PANE_MARGIN_RIGHT = 0;

    public static final int FORM_PANE_HORIZONTAL_SPACING = FORM_BODY_HORIZONTAL_SPACING;

    public static final int FORM_PANE_VERTICAL_SPACING = FORM_BODY_VERTICAL_SPACING;

    public static final int FORM_PANE_MARGIN_HEIGHT = 0;

    public static final int FORM_PANE_MARGIN_WIDTH = 0;

    /**
     * Qiang.Zhang.Adolf@gmail.com AbstractFormPage constructor comment.
     * 
     * @param editor
     * @param id
     * @param title
     */
    public AbstractFormPage(FormEditor editor, String id, String title) {
        super(editor, id, title);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.forms.editor.FormPage#createFormContent(org.eclipse.ui.forms.IManagedForm)
     */
    @Override
    protected void createFormContent(IManagedForm managedForm) {
        final ScrolledForm form = managedForm.getForm();
        FormToolkit toolkit = managedForm.getToolkit();
        toolkit.decorateFormHeading(form.getForm());

        // check to see if our form parts are contributing actions
        IFormPart[] parts = managedForm.getParts();
        for (int i = 0; i < parts.length; i++) {
            if (parts[i] instanceof IAdaptable) {
                IAdaptable adapter = (IAdaptable) parts[i];
                IAction[] actions = (IAction[]) adapter.getAdapter(IAction[].class);
                if (actions != null) {
                    for (int j = 0; j < actions.length; j++) {
                        form.getToolBarManager().add(actions[j]);
                    }
                }
            }
        }
        form.updateToolBar();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createFormGridLayout".
     * 
     * @param makeColumnsEqualWidth
     * @param numColumns
     * @return
     */
    public final GridLayout createFormGridLayout(boolean makeColumnsEqualWidth, int numColumns) {
        GridLayout layout = new GridLayout();

        layout.marginHeight = FORM_BODY_MARGIN_HEIGHT;
        layout.marginWidth = FORM_BODY_MARGIN_WIDTH;

        layout.marginTop = FORM_BODY_MARGIN_TOP;
        layout.marginBottom = FORM_BODY_MARGIN_BOTTOM;
        layout.marginLeft = FORM_BODY_MARGIN_LEFT;
        layout.marginRight = FORM_BODY_MARGIN_RIGHT;

        layout.horizontalSpacing = FORM_BODY_HORIZONTAL_SPACING;
        layout.verticalSpacing = FORM_BODY_VERTICAL_SPACING;

        layout.makeColumnsEqualWidth = makeColumnsEqualWidth;
        layout.numColumns = numColumns;

        return layout;
    }

    /**
     * For composites used to group sections in left and right panes.
     * 
     * @param makeColumnsEqualWidth
     * @param numColumns
     * @return
     */
    public final GridLayout createFormPaneGridLayout(boolean makeColumnsEqualWidth, int numColumns) {
        GridLayout layout = new GridLayout();

        layout.marginHeight = FORM_PANE_MARGIN_HEIGHT;
        layout.marginWidth = FORM_PANE_MARGIN_WIDTH;

        layout.marginTop = FORM_PANE_MARGIN_TOP;
        layout.marginBottom = FORM_PANE_MARGIN_BOTTOM;
        layout.marginLeft = FORM_PANE_MARGIN_LEFT;
        layout.marginRight = FORM_PANE_MARGIN_RIGHT;

        layout.horizontalSpacing = FORM_PANE_HORIZONTAL_SPACING;
        layout.verticalSpacing = FORM_PANE_VERTICAL_SPACING;

        layout.makeColumnsEqualWidth = makeColumnsEqualWidth;
        layout.numColumns = numColumns;

        return layout;
    }
}
